package com.cskaoyan.market.admin.service.impl;

import org.apache.ibatis.session.SqlSession;

import com.cskaoyan.market.admin.service.MarketAdminService;
import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.db.domain.MarketAdminExample;
import com.cskaoyan.market.db.mapper.MarketAdminMapper;
import com.cskaoyan.market.util.MyBatisUtil;

/**
 * @ClassName MarketAdminServiceImpl
 * @Description: TODO
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2023/3/17 14:44
 * @Version V1.0
 **/
public class MarketAdminServiceImpl implements MarketAdminService {
    @Override
    public MarketAdmin login(String username, String password) {
        //调用dao代码来进行查询
        //SqlSession一定不能写成成员变量
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketAdminMapper adminMapper = sqlSession.getMapper(MarketAdminMapper.class);
        MarketAdminExample adminExample = new MarketAdminExample();
        adminExample.or().andUsernameEqualTo(username).andPasswordEqualTo(password);
        MarketAdmin admin = adminMapper.selectOneByExample(adminExample);
        sqlSession.commit();
        //一定要记得及时close，否则会出现一些问题
        sqlSession.close();
        return admin;
    }

    @Override
    public void updateById(MarketAdmin admin) {
        SqlSession session = MyBatisUtil.getSession();
        MarketAdminMapper adminMapper = session.getMapper(MarketAdminMapper.class);
        adminMapper.updateByPrimaryKeySelective(admin);
        //记得提交事务
        session.commit();
        session.close();
    }
}